import request from '@/router/axios';
import Mock from 'mockjs'; 

// sort 排序    status 是否可用 0不可用 1可用    createTime 添加时间
let _list = [
    {id: 1, name: "质量问题", sort: 1, status: 1, createTime: "2021-10-17 02:00:45"},
    {id: 2, name: "尺码太大", sort: 1, status: 1, createTime: "2021-10-17 02:01:03"},
    {id: 3, name: "颜色不喜欢", sort: 1, status: 1, createTime: "2021-10-17 02:01:13"},
    {id: 4, name: "7天无理由退货", sort: 1, status: 1, createTime: "2021-10-17 02:01:47"},
    {id: 5, name: "价格问题", sort: 1, status: 0, createTime: "2021-10-17 02:01:57"},
]

//获取时间
function getCurrentDate() {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth();//得到月份
    var date = now.getDate();//得到日期
    var day = now.getDay();//得到周几
    var hour = now.getHours();//得到小时
    var minu = now.getMinutes();//得到分钟
    var sec = now.getSeconds();//得到秒
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var time = "";
    time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
    return time;
}

// 获取数据列表
export const reasonList = (data) =>request({
    url:'/reasonList',
    method:'get',
    data
})
Mock.mock(/\/reasonList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

//添加理由
export const addReason = (data) =>request({
    url:'/addReason',
    mothed:'post',
    data
});
Mock.mock(/\/addReason/,config=>{
    var ids = Mock.mock({"id|6-20":1000});
    let datas = JSON.parse(config.body)
    let {name} = datas;
    if(name){
        _list.unshift({
            id:ids.id,
            name,
            sort:datas.sort?datas.sort:1,
            status:datas.status?datas.status:1,
            createTime:getCurrentDate()
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除理由
export const detReason = (data) =>request({
    url:'/detReason',
    mothed:'delete',
    data
})
Mock.mock(/\/detReason/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        _list.splice(_list.findIndex(item=>item.id==id),1)
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询理由  根据理由名称
export const queryReason = (data) =>request({
    url:'/queryReason',
    mothed:'get',
    data
})
Mock.mock(/\/queryReason/,config=>{
    let {name} = JSON.parse(config.body);
    let arr = [];
    if(name){
        arr = _list.filter((item,index)=>item.name.indexOf(name)>=0);
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 修改理由数据
export const editReason = (data) =>request({
    url:'/editReason',
    mothed:'put',
    data
})
Mock.mock(/\/editReason/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.name=datas.name?datas.name:item.name
                item.sort=datas.sort?datas.sort:item.sort
                item.createTime=getCurrentDate()
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

//修改是否可用
export const editReasonShow = (data) =>request({
    url:'/editReasonShow',
    mothed:'put',
    data
})
Mock.mock(/\/editReasonShow/,config=>{
    let datas = JSON.parse(config.body);
    let {id,status} = datas;
    if(id){
        for(let item of allList){
            if(item.id==id){
                item.status=status
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})